<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('生产记录列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>销售编号：</label>
                                <input type="text" name="code"/>
                            </li>
                            <li>
                                <label>产品编号：</label>
                                <input type="text" name="pCode"/>
                            </li>
                            <li>
                                <label>产品名称：</label>
                                <input type="text" name="pName"/>
                            </li>

                            <li>
                                <label>状态：</label>
                                <input type="text" name="state"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "business/sales";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "销售记录",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'code',
                    title: '销售编号'
                },
                {
                    field: 'pCode',
                    title: '产品编号'
                },
                {
                    field: 'pName',
                    title: '产品名称'
                },
                {
                    field: 'num',
                    title: '数量'
                },
                {
                    field: 'unitPrice',
                    title: '单价'
                },
                {
                    field: 'totalPrice',
                    title: '总价'
                },
                {
                    field: 'createBy',
                    title: '创建人'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'state',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return '<span class="badge badge-info" style="'+ getStateCss(value) +'">' + getStateLabel(value) + '</span>';
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if ("NEW" == row.state || "CHECK_FAILED" == row.state){
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="updatePurchase(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        }
                        if ("CHECKING" == row.state){
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="check(\'' + row.id + '\')"><i class="fa fa-adjust"></i>审核</a> ');
                        }
                        if ("CHECK_SUCCESS" == row.state){
                            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="sales(\'' + row.id + '\', \'OUTING\')"><i class="fa fa-instagram"></i>出库</a> ');
                        }
                        if ("CHECK_FAILED" == row.state){
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="failReason(\'' + row.reason + '\')"><i class="fa fa-amazon"></i>不通过原因</a> ');
                        }
                        if ("OUTING" == row.state){
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="sales(\'' + row.id + '\', \'OUTING_SUCCESS\')"><i class="fa fa-address-card"></i>完成出库</a> ');
                        }
                        if ("OUTING_SUCCESS" == row.state){
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="sales(\'' + row.id + '\', \'SALE_SUCCESS\')"><i class="fa fa-address-card"></i>销售完成</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        /** 审核弹窗 */
        function check(id) {
            open("审核页面", prefix + "/check/" + id, 800, 320)
        }

        function open(title, url, width, height, callback, full) {
            //如果是移动端，就使用自适应大小弹窗
            if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
                width = 'auto';
                height = 'auto';
            }
            if ($.common.isEmpty(title)) {
                title = false;
            }
            if ($.common.isEmpty(url)) {
                url = "/404.html";
            }
            if ($.common.isEmpty(width)) {
                width = 800;
            }
            if ($.common.isEmpty(height)) {
                height = ($(window).height() - 50);
            }
            if ($.common.isEmpty(callback)) {
                callback = function (index, layero) {
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.submitHandler(index, layero);
                }
            }
            let index = layer.open({
                type: 2,
                area: [width + 'px', height + 'px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: title,
                content: url,
                btn: ['确定', '关闭'],
                // 弹层外区域关闭
                shadeClose: false,
                yes: callback,
                cancel: function (index) {
                    return true;
                }
            });
        }

        /** 审核不通过原因弹窗 */
        function failReason(reason) {
            layer.open({
                title: "审核失败原因【修改后可再次提交】",
                area: ['500px', '200px'],
                content: '<div class="row" style="width: 6000px;  margin-left:7px; margin-top:10px;">'
                    + '<div class="col-sm-12" style="margin-top: 10px">'
                    + '<span>' + reason + '</span>'
                    + '</div>'
                    + '</div>',
                btn: ['关闭'],
                yes: function (index, layerObject) {
                    layer.close(index);
                }
            });
        }

        /** 状态修改 */
        function sales(id, state) {
            var config = {
                url: prefix + "/sales",
                type: "post",
                dataType: "json",
                data: {'id': id, 'state':state},
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                    $.modal.closeLoading();
                },
                success: function(result) {
                    if (result.code == 0){
                        $.modal.alertSuccess("操作成功！");
                        $.table.refresh();
                    } else{
                        $.modal.alertError("操作失败，请重试！");
                        $.table.refresh();
                    }
                }
            };
            $.ajax(config)
        }
    </script>
</body>
</html>